package com.station.Activitys.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.station.Activitys.R;
import com.station.Activitys.activity.base.BaseActivity;
import com.station.Activitys.adapter.ImageDisplayMagAdapter;
import com.station.Activitys.bean.Survey;
import com.station.Activitys.bean.SurveyDetails;
import com.station.Activitys.http.manager.WorkManager;
import com.station.Activitys.utils.Constant;
import com.station.Activitys.utils.StringUtils;
import com.station.Activitys.utils.ToastUtils;
import com.zhy.autolayout.AutoLinearLayout;

import org.simple.eventbus.Subscriber;
import org.simple.eventbus.ThreadMode;

import butterknife.Bind;

/**
 * 项目名称：基站管理平台 手机app
 * 类描述：基站 工单详情
 * 创建人：张飞祥
 * 创建时间：2018/8/30 9:37
 * 修改人：张飞祥
 * 修改时间：2018/8/30 9:37
 * 修改备注：
 */
public class WorkDetailsActivity extends BaseActivity {

    //TODO 标题
    //返回按钮
    @Bind(R.id.iv_return) ImageView ivReturn;
    //标题
    @Bind(R.id.tv_title) TextView tvTitle;

    //TODO 基站 基础信息
    //基站名称
    @Bind(R.id.tv_name) TextView tvName;
    //基站状态
    @Bind(R.id.tv_state) TextView tvState;
    //省份
    @Bind(R.id.tv_province) TextView tvProvince;
    //城市
    @Bind(R.id.tv_city) TextView tvCity;
    //网格
    @Bind(R.id.tv_grid) TextView tvGrid;
    //厂家
    @Bind(R.id.tv_vender) TextView tvVender;
    //物理站址名称
    @Bind(R.id.tv_physical_name) TextView tvPhysicalName;
    //物理站址地址
    @Bind(R.id.tv_physical_address) TextView tvPhysicalAddress;
    //设计院名称
    @Bind(R.id.tv_ui_yard) TextView tvUiYard;
    //区域类型
    @Bind(R.id.tv_area_type) TextView tvAreaType;
    //主场景类型
    @Bind(R.id.tv_main_scene_type) TextView tvMainSceneType;
    //站型
    @Bind(R.id.tv_station_model) TextView tvStationModel;
    //站址经度
    @Bind(R.id.tv_gps_x) TextView tvGpsX;
    //站址维度
    @Bind(R.id.tv_gps_y) TextView tvGpsY;
    //优先级
    @Bind(R.id.tv_priority) TextView tvPriority;
    //站址所属
    @Bind(R.id.tv_territoriality) TextView tvTerritoriality;

    //TODO 规划信息
    //总布局
    @Bind(R.id.layout_survey_msg) AutoLinearLayout layoutSurveyMsg;
    //TODO 公用信息
    //自动获取的经纬度
    @Bind(R.id.tv_gps) TextView tvGps;
    //手动输入的经纬度
    @Bind(R.id.tv_manual_gps) TextView tvManualGps;
    //信号强度
    @Bind(R.id.tv_signal) TextView tvSignal;
    //工程期属
    @Bind(R.id.tv_project) TextView tvProject;
    //图片列表
    @Bind(R.id.rv_img_list) RecyclerView rvImgList;

    //TODO 室外站
    //非室分总布局
    @Bind(R.id.layout_not_room) AutoLinearLayout layoutNotRoom;
    //天线挂高
    @Bind(R.id.tv_antenna_height) TextView tvAntennaHeight;
    //天线方位角
    @Bind(R.id.tv_antenna_horn) TextView tvAntennaHorn;
    //是否BBU集中部署
    @Bind(R.id.tv_bbu) TextView tvBbu;
    //扇区配置
    @Bind(R.id.tv_sector) TextView tvSector;
    //RRU规划功率
    @Bind(R.id.tv_rru_plan) TextView tvRruPlan;
    //RRU收发需求
    @Bind(R.id.tv_rru_receive) TextView tvRruReceive;
    //是否远程电下倾调整
    @Bind(R.id.tv_adjust) TextView tvAdjust;

    //TODO 室内站
    //室分总布局
    @Bind(R.id.layout_room)
    AutoLinearLayout layoutRoom;
    //室分子场景类型
    @Bind(R.id.tv_scene_type) TextView tvSceneType;
    //频段
    @Bind(R.id.tv_frequency_channel) TextView tvFrequencyChannel;
    //室分类型
    @Bind(R.id.tv_type) TextView tvType;
    //覆盖建筑、楼层或位置
    @Bind(R.id.tv_cover_land) TextView tvCoverLand;
    //覆盖面积
    @Bind(R.id.tv_cover_acreage) TextView tvCoverAcreage;
    //有源室分的PRRU信源个数
    @Bind(R.id.tv_yes_prru) TextView tvYesPrru;
    //无源室分的PRRU信源个数
    @Bind(R.id.tv_no_pru) TextView tvNoPru;
    //无源室分单双路类型
    @Bind(R.id.tv_no_pru_type) TextView tvNoPruType;

    //TODO 未通过原因
    //总布局
    @Bind(R.id.layout_reason) AutoLinearLayout layoutReason;
    //原因
    @Bind(R.id.tv_reason) TextView tvReason;

    //TODO 变量
    //初始化网络请求
    public WorkManager mApiManager;
    //基站规划 实体类
    private Survey mSurvey = null;

    @Override
    protected void initUI() {
        setContentView(R.layout.act_work_details);
        //网络请求接口 常驻方法
        mApiManager = mApiManager.getWorkApiManager(this, this.getTaskId());

        //设置标题
        tvTitle.setText("详情信息");
        //显示返回按钮
        ivReturn.setVisibility(View.VISIBLE);
        //返回按钮点击事件
        ivReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        //解决 滑动冲突
        rvImgList.setNestedScrollingEnabled(false);

        //获取从上个页面 传递过来的 实体类
        Bundle bundle = this.getIntent().getExtras();
        //获取 规划 实体类
        mSurvey = (Survey) bundle.getSerializable(Constant.INTENT_SURVEY_BEAN);

        /**
         * 填充 数据
         */
        if (mSurvey != null){
            //基站名称
            tvName.setText(mSurvey.getPhysicalStationName());
            //基站状态
            tvState.setText(mSurvey.getState());
            //省份
            tvProvince.setText(mSurvey.getProvince());
            //城市
            tvCity.setText(mSurvey.getLocalNetworkAddress());
            //网格
            tvGrid.setText(mSurvey.getGrid());
            //厂家
            tvVender.setText(mSurvey.getVender());
            //物理站址名称
            tvPhysicalName.setText(mSurvey.getPhysicalStationName());
            //物理站址地址
            tvPhysicalAddress.setText(mSurvey.getAddressOfPhysicalStation());
            //区域类型
            tvAreaType.setText(mSurvey.getAreaType());
            //主场景类型
            tvMainSceneType.setText(mSurvey.getMainSceneType());
            //站型
            tvStationModel.setText(mSurvey.getStationModel());
            //站址经度
            tvGpsX.setText(mSurvey.getGPS_X());
            //站址维度
            tvGpsY.setText(mSurvey.getGPS_Y());
            //优先级
            tvPriority.setText(mSurvey.getPriority());
            //站址所属
            tvTerritoriality.setText(mSurvey.getHighSpeedRailwayStation());
        }

        //开始网络请求获取 数据
        initShowTimeDialog("正在加载数据，请稍后....",this);
        mApiManager.workDetails(mSurvey.getId());
    }

    /**
     * 获取基站信息数据 报错
     */
    @Subscriber(tag = Constant.BUS_WORK_DETAILS_ERROR, mode = ThreadMode.MAIN)
    public void initHttpError(String httpError) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //销毁弹窗
                mTimeDialog.dismiss();
                //弹出提示
                ToastUtils.showShort(WorkDetailsActivity.this, Constant.NETWORK_ERROR_MSG);
            }
        }, 1000);
    }

    /**
     * 获取基站信息数据 报错
     */
    @Subscriber(tag = Constant.BUS_WORK_DETAILS, mode = ThreadMode.MAIN)
    public void initHttpData(final SurveyDetails details) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //销毁弹窗
                mTimeDialog.dismiss();

                //TODO 判断 是否 有规划详情，有则显示，否则 不显示
                if (details != null){
                    //显示 规划详情View
                    layoutSurveyMsg.setVisibility(View.VISIBLE);

                    //站型
                    String model = details.getStationModel();
                    //开始填充数据
                    //TODO 基础数据
                    //设计院名称
                    tvUiYard.setText(details.getDesignInstitudeName());
                    //自动获取的经纬度
                    tvGps.setText("经度："+details.getAutoGpsX()+"     纬度："+details.getAutoGpsY());
                    //手动输入的经纬度
                    tvManualGps.setText("经度："+details.getManualInputGpsX()+"     纬度："+details.getManualInputGpsY());
                    //信号强度
                    tvSignal.setText(details.getSignalIntensity() + " dBm");
                    //工程期属
                    tvProject.setText(details.getNumOfProject());
                    //图片列表
                    ImageDisplayMagAdapter adapter = new ImageDisplayMagAdapter(WorkDetailsActivity.this,details.getImgs(),false);
                    //添加 适配器 样式
                    rvImgList.setLayoutManager(new LinearLayoutManager(WorkDetailsActivity.this, LinearLayoutManager.VERTICAL, false));
                    //添加 适配器
                    rvImgList.setAdapter(adapter);

                    //TODO 逻辑站型数据
                    if (model.equals(Constant.STATION_TYPE_NO_ROOM))
                        initShowRoom(details);
                    else
                        initShowNotRoom(details);

                    //TODO 判断是否显示未通过原因
                    if (StringUtils.isContainStr(mSurvey.getState(),"未通过")){
                        //显示布局
                        layoutReason.setVisibility(View.VISIBLE);
                        //反填原因
                        tvReason.setText(details.getAuditingInfoRemark());
                    }
                }
            }
        }, 1000);
    }

    /**
     * 室外站 数据填充
     */
    private void initShowNotRoom(SurveyDetails details){
        //显示非室分页面
        layoutNotRoom.setVisibility(View.VISIBLE);
        //天线挂高
        tvAntennaHeight.setText(details.getHeight());
        //天线方位角
        tvAntennaHorn.setText(details.getAntennaAzimuth());
        //是否BBU集中部署
        tvBbu.setText(details.getIsBBUCentralizedDeployment());
        //扇区配置
        tvSector.setText(details.getSectorAllocation());
        //RRU规划功率
        tvRruPlan.setText(details.getRRUPlanningPower());
        //RRU手法需求
        tvRruReceive.setText(details.getRRUTransceiverRequirements());
        //是否远程电下倾调整
        tvAdjust.setText(details.getRemotePowerDownAdjustmentIsRequired());
    }

    /**
     * 是内站 数据填充
     */
    private void initShowRoom(SurveyDetails details){
        //显示室分页面
        layoutRoom.setVisibility(View.VISIBLE);
        //室分子场景类型
        tvSceneType.setText(details.getChamberMolecularSceneType());
        //频段
        tvFrequencyChannel.setText(details.getFrequencyChannel());
        //室分类型
        tvType.setText(details.getCompartmentType());
        //覆盖建筑、楼层或位置
        tvCoverLand.setText(details.getCoveringBuildingsFloorsOrLocations());
        //覆盖面积
        tvCoverAcreage.setText(details.getCoverageArea());
        //有源室分的PRRU信源个数
        tvYesPrru.setText(details.getPRRUSourcesNum());
        //无源室分的PRRU信源个数
        tvNoPru.setText(details.getNumberOfRRUSourcesForPassiveCellDivision());
        //无源室分单双路类型
        tvNoPruType.setText(details.getPassiveCellTypeSingleAndDoubleChannelType());
    }
}
